﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.AppInfo.controls
{
    public partial class insert : UserControl
    {
        public insert()
        {
            InitializeComponent();
            this.Loaded += (obj, e) =>
            {
                OnLoad();
            };
        }

        public JuCloudy.Sys.App.AppInfo.MainPage MainPage
        {
            get;
            set;
        }

        public JuCloudy.Data.Entity.SysAppInfo Entity { get; set; }

        public ObservableCollection<JuCloudy.Data.Entity.SysCategory> Categories { get; set; }

        private void OnLoad()
        {
            foreach (var c in Categories)
            {
                this.select_cate.Items.Add(new ComboBoxItem() { Content = c.CategoryName, Tag = c.CategoryId });
            }
            if (this.Entity != null)
            {
                this.txt_appassembly.Text = Entity.AppAssembly;
                this.txt_appintroduction.Text = Entity.AppIntroduction;
                this.txt_appname.Text = Entity.AppName;
                this.txt_mainpagename.Text = Entity.AppMainPageName;
                this.select_cate.SelectedItem = this.select_cate.Items.First(p => (int)((ComboBoxItem)p).Tag == Entity.CategoryId);
                this.chk_enable.IsChecked = Entity.IsEnable;
                this.chk_visable.IsChecked = Entity.IsVisable;
            }

            this.btn_submit.Click += (obj, e) =>
            {
                if (String.IsNullOrEmpty(this.txt_appassembly.Text) ||
                    String.IsNullOrEmpty(this.txt_appintroduction.Text) ||
                    String.IsNullOrEmpty(this.txt_appname.Text) ||
                    String.IsNullOrEmpty(this.txt_mainpagename.Text) ||
                    this.select_cate.SelectedItem == null ||
                    this.chk_enable.IsChecked == null ||
                    this.chk_visable.IsChecked == null)
                {
                    throw new Exception("表单不能为空！");
                }
                else
                {
                    var model = new Data.Entity.SysAppInfo()
                    {
                        AppAssembly = this.txt_appassembly.Text,
                        AppIntroduction = txt_appintroduction.Text,
                        AppName = txt_appname.Text,
                        AppMainPageName = txt_mainpagename.Text,
                        CategoryId = (int)((ComboBoxItem)this.select_cate.SelectedItem).Tag,
                        IsEnable = (bool)this.chk_enable.IsChecked,
                        IsVisable = (bool)this.chk_visable.IsChecked,
                        CreateBy = 0,
                        CreateDate = DateTime.Now,
                        CreateDateTimestamp = 0,
                        LastUpdataBy = 0,
                        LastUpdataDate = DateTime.Now,
                        LastUpdataDateTimestamp = 0
                    };
                    if (Entity != null)
                        model.AppId = Entity.AppId;

                    MainPage.insert(model);
                }
            };
        }
    }
}
